<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">编辑资料</block>
		</cu-custom>
		<form>
			<u-gap height="15" bgColor="#f1f1f1"></u-gap>
			<view class="cu-form-group">
				<view class="title">商户规模</view>
				<input placeholder="请选择商户规模" name="input" :disabled="true" v-model="myFormData.shopScale.name" @click="showPicker=true"></input>
			</view>
			<view class="padding flex flex-direction">
				<button class="cu-btn bg-blue lg" @click="onSubmit">提交</button>
			</view>
		</form>
		<u-picker :show="showPicker" title="选择商户规模" :columns="scales" :defaultIndex="selectedIndex" keyName="name" :closeOnClickOverlay="true" @confirm="changePicker" @close="cancelPicker" @cancel="cancelPicker"></u-picker>
		<!-- 加载弹窗 -->
		<view class="cu-load load-modal" v-if="loading">
			<!-- <view class="cuIcon-emojifill text-orange"></view> -->
			<image src="https://mini.wm-mis.com/img/static/car.png" mode="aspectFit" class="round"></image>
			<view class="gray-text">加载中...</view>
		</view>
	</view>
</template>

<script>
	import api from '@/api/api.js'
	export default {
		data() {
			return {
				loading: false,
				param: null,
				myFormData: {
					shopScale: {
						name: '',
						label: ''
					},
					shopId: '',
				},
				showPicker: false,
				selectedIndex: [0],
				scales: [[
					{
						name: '小型',
						label: 'SMALL'
					},
					{
						name: '中型',
						label: 'MEDIUM'
					},
					{
						name: '大型',
						label: 'BIG'
					}
				]]
			};
		},
		async onLoad(options){
			this.param = JSON.parse(options.item)
			this.myFormData.shopId = this.param.id
			if(this.param.shopScale == 'SMALL'){
				this.myFormData.shopScale = {
					name: '小型',
					label: 'SMALL'
				}
				this.selectedIndex = [0]
			}
			else if(this.param.shopScale == 'MEDIUM'){
				this.myFormData.shopScale = {
					name: '中型',
					label: 'MEDIUM'
				}
				this.selectedIndex = [1]
			}
			else if(this.param.shopScale == 'BIG'){
				this.myFormData.shopScale = {
					name: '大型',
					label: 'BIG'
				}
				this.selectedIndex = [2]
			}
		},
		methods: {
			async onSubmit() {
				let that = this
				let myForm = this.myFormData
				if (!myForm.shopScale.name) {
					uni.showToast({
						title: '请选择商户规模',
						icon: 'none',
						duration: 1500
					});
					return false
				}
				
				that.loading = true
				let res = await api.shopScaleEdit({
					shopId: this.myFormData.shopId,
					scale: this.myFormData.shopScale.label
				})
				if(res.data.code == 200 && res.data.success){
					uni.setStorageSync('entryDetail', 'peopleDetail')//添加操作标记，返回时需要刷新
					uni.showToast({
						title: '操作成功',
						icon: 'success',
						duration: 2000
					})
					
					that.loading = false
					setTimeout(()=>{
						uni.navigateBack();
					}, 2000)
				}
				that.loading = false
			},
			async changePicker(e) {
				const {
					indexs,
					value
				} = e
			
				let that = this;
				that.showPicker = false;
				
				//选中的设备
				let selectIndex = indexs[0];
				that.selectedIndex = [selectIndex];
				let scale = that.scales[0][selectIndex];
				that.myFormData.shopScale = scale
			},
			cancelPicker() {
				this.showPicker = false;
			},
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
</style>